package com.bookstore.controller;

import com.bookstore.common.Result;
import com.bookstore.entity.ShoppingCart;
import com.bookstore.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
@Api(tags = "购物车管理接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping
    @ApiOperation("添加商品到购物车")
    public Result<Boolean> addToCart(
            @ApiParam("用户ID") @RequestParam Integer userId,
            @ApiParam("图书ID") @RequestParam Integer bookId,
            @ApiParam("数量") @RequestParam Integer quantity) {
        return Result.success(shoppingCartService.addToCart(userId, bookId, quantity));
    }

    @PutMapping("/{cartId}/quantity")
    @ApiOperation("更新购物车商品数量")
    public Result<Boolean> updateQuantity(
            @ApiParam("购物车ID") @PathVariable Integer cartId,
            @ApiParam("数量") @RequestParam Integer quantity) {
        return Result.success(shoppingCartService.updateQuantity(cartId, quantity));
    }

    @DeleteMapping("/{cartId}")
    @ApiOperation("从购物车删除商品")
    public Result<Boolean> removeFromCart(
            @ApiParam("购物车ID") @PathVariable Integer cartId) {
        return Result.success(shoppingCartService.removeFromCart(cartId));
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("获取用户购物车列表")
    public Result<List<ShoppingCart>> getUserCart(
            @ApiParam("用户ID") @PathVariable Integer userId) {
        return Result.success(shoppingCartService.getUserCart(userId));
    }

    @DeleteMapping("/user/{userId}/clear")
    @ApiOperation("清空用户购物车")
    public Result<Boolean> clearCart(
            @ApiParam("用户ID") @PathVariable Integer userId) {
        return Result.success(shoppingCartService.clearCart(userId));
    }
} 